<?php
/*
Plugin Name: Taxonomy Example
Plugin URI: http://cdiabu.com
Description: A plugin to demonstrate WordPress taxonomies.
Version: 1.0
Author: gradyetc
Author URI: http://grady-etc.com
License: GPL2
*/

// WordPress Hooks
add_action( 'init', 'te_init' );
add_filter( 'the_content', 'te_append_terms' );

register_activation_hook( __FILE__, 'te_on_activate' );

/**
 * Activation hook
 *
 * Run init callback (register post types and taxonomy) and then flush the rewrite rules
 * 
 */
function te_on_activate() {
        
        // Register post type(s) and taxonomy
        te_init();
        
        // Flush rewrite cache
        flush_rewrite_rules();

}

/**
 * You MUST register custom post types and taxonomy in the init callback
 *
 */
function te_init() {
        
        $genre_args = array( 
                'hierarchical' => true,
                'query_var' => 'genre',
                'rewrite' => array(
                        'slug' => 'works/by-genre',
                        'with_front' => false,
                ),
                'labels' => array(
                        'name' => 'Genres',
                        'singular_name' => 'Genre'
                )
        );
        
        // Register taxonomy first
        register_taxonomy( 'genre', array( 'work' ), $genre_args );
        
        $work_args = array(
                'public' => true,
                'query_var' => 'work',
                'rewrite' => array(
                        'slug' => 'works',
                        'with_front' => false
                ),
                'has_archive' => true,
                'supports' => array(
                        'title',
                        'thumbnail',
                        'editor',
                ),
                'labels' => array(
                        'name' => 'Works',
                        'singular_name' => 'Work',
                )
        );
        
        // Register post type second
        register_post_type( 'work', $work_args );
        
}

/**
 * Demonstrate taxonomy by appending current post taxonomy terms to content
 *
 */
function te_append_terms($content) {
        
        // Get the term list for the 'Genre' taxonomy for the current post
        $current_terms = get_the_term_list( get_the_ID(), 'genre' );
        
        // Append to the post conent if we have any
        if( $current_terms ) { 
                $content .= $current_terms;
        }
        
        // Always return for filters!
        return $content;
}

?>